'use strict'

const fs = require('fs')
const { Sequelize, DataTypes, Op } = require('sequelize')
const { db_database, db_dialect, db_host, db_pwd, db_username } = require('../config/db')

const sequelize = new Sequelize(db_database, db_username, db_pwd, {
    host: db_host,
    dialect: db_dialect
})

let files = fs.readdirSync(__dirname)
let resFiles = files.filter(item => {
    return item.endsWith('.js') && item != 'index.js'
})

let resObj = {}
resFiles.forEach(item => {
    let modelName = item.replace('.js', '')
    let tmpObj = require(__dirname + '/' + item)
    let tableName = modelName.toLowerCase()
    resObj[modelName] = sequelize.define(tableName, tmpObj)
})
resObj.Op = Op

resObj.sync = async () => {
    sequelize.sync({ force: true }).then(() => {
        resObj.Shop.bulkCreate([
            {
                name: '短袖',
                price: 88
            },
            {
                name: '泳裤',
                price: 45
            },
            {
                name: '手表',
                price: 1288
            },
        ])
    })
}

module.exports = resObj